package sxt.servlet;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import sxt.entity.Announcement;
import sxt.service.AnnouncementService;
import sxt.service.impl.AnnouncementServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/sxt/AnnouncementServlet")
public class AnnouncementServlet extends BaseServlet {

    AnnouncementService announcementService=new AnnouncementServiceImpl();

    private void AnnouncementFindAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //1.调用service方法,获取数据
        List<Announcement> allComment = announcementService.findAllComment();
            //将日期格式进行转换,变为yyyy-MM-dd HH-mm-ss
        /*创建一个能转换日期的Gson对象  格式yyyy-MM-dd HH:mm:ss*/

        for (Announcement announcement:allComment){
            announcement.setTime(new Date());
        }

        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();

        //2.将list&普通数据进行数据转换
        String json = gson.toJson(allComment);

        //3.创建write对象,给页面写入json字符串数据,把这个数据保存到响应中,并响应给用户
        resp.getWriter().write(json);
    }

    private void addAnnouncementServlet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        String announcement = req.getParameter("announcement");

        //
        int add = announcementService.addAnnocement(announcement, new Date());

        //
        req.setAttribute("add",add);
        resp.sendRedirect("main.jsp");
    }

    private void findOneAnnouncementServlet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        int id = Integer.parseInt(req.getParameter("id"));
//        System.out.println(id);
        //
        Announcement announcement = announcementService.findOneAnnouncement(id);
//        System.out.println(announcement.getAnnouncement());
    //
        //req.setAttribute("announcement",announcement);
        req.getSession().setAttribute("announcement",announcement);
//        req.getRequestDispatcher("/xiugaiAnnoucement.jsp").forward(req,resp);
        resp.sendRedirect("/xiugaiAnnoucement.jsp");
}

    private void changeAnnouncementServlet(HttpServletRequest req, HttpServletResponse resp)throws IOException {

        int id = Integer.parseInt(req.getParameter("id"));
        String announcement = req.getParameter("announcement");

        //
        int change = announcementService.changeAnnouncement(id,announcement);

        //
        req.setAttribute("change",change);
        resp.sendRedirect("/announcementAdmin.jsp");
    }

    private void removeAnnouncement(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = Integer.parseInt(req.getParameter("id"));
//        System.out.println(id);前端调用·后台方法也会打印输出语句，可以用此特性测试

        //
        int remove = announcementService.removeAnnouncement(id);

        //
        req.setAttribute("remove",remove);
        req.getRequestDispatcher("/announcementAdmin.jsp").forward(req,resp);
        //resp.sendRedirect("/announcementAdmin.jsp");
    }

}
